﻿using Commons;
using EntityFramework;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Bill.EntityFramework.Data
{
    public class DbInitializer
    {
        public static void Initialize(MyDbContext context, IHostingEnvironment env)
        {
            //context.Database.EnsureDeleted();
            //创建数据库
            context.Database.EnsureCreated();

            if (context.T_SYS_Province.Count() <= 0)
            {
                string provincesPath = env.WebRootPath + "\\Data\\provinces.json";
                using (System.IO.StreamReader file = System.IO.File.OpenText(provincesPath))
                {
                    string str = file.ReadToEnd();

                    List<T_SYS_Province> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T_SYS_Province>>(str);
                    foreach (var item in list)
                    {
                        context.T_SYS_Province.Add(item);
                    }

                }

                string citiesPath = env.WebRootPath + "\\Data\\cities.json";
                using (System.IO.StreamReader file = System.IO.File.OpenText(citiesPath))
                {
                    string str = file.ReadToEnd();

                    List<T_SYS_City> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T_SYS_City>>(str);
                    foreach (var item in list)
                    {
                        context.T_SYS_City.Add(item);
                    }

                }

                string areasPath = env.WebRootPath + "\\Data\\areas.json";
                using (System.IO.StreamReader file = System.IO.File.OpenText(areasPath))
                {
                    string str = file.ReadToEnd();

                    List<T_SYS_Area> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T_SYS_Area>>(str);
                    foreach (var item in list)
                    {
                        context.T_SYS_Area.Add(item);
                    }

                }

                List<T_PrizeModel> listPrize = new List<T_PrizeModel>() {
                    new T_PrizeModel(){ Code=Prize.ToTime.ToString(),CreateDate=DateTime.Now,StateCode=StateCode.Enable.ToString(),Title="按时间开奖"},
                    new T_PrizeModel(){ Code=Prize.ToNum.ToString(),CreateDate=DateTime.Now,StateCode=StateCode.Enable.ToString(),Title="按人数开奖"},
                    //new T_PrizeModel(){ Code=Prize.ToManual.ToString(),CreateDate=DateTime.Now,StateCode=StateCode.Enable.ToString(),Title="手动开奖"},
                    //new T_PrizeModel(){ Code=Prize.ToLocale.ToString(),CreateDate=DateTime.Now,StateCode=StateCode.Enable.ToString(),Title="现场开奖"},
                };
                List<T_ExchangeModel> listExchange = new List<T_ExchangeModel>() {
                    new T_ExchangeModel(){ Code=Exchange.Online.ToString(),CreateDate=DateTime.Now,StateCode=StateCode.Enable.ToString(),Title="线上兑奖"},
                    new T_ExchangeModel(){ Code=Exchange.Offline.ToString(),CreateDate=DateTime.Now,StateCode=StateCode.Enable.ToString(),Title="线下兑奖"},
                };
                List<T_PointsRule> listRule = new List<T_PointsRule>() {
                    new T_PointsRule(){ Code=PointsRule.JOIN.ToString(),Limit=999,CreateDate=DateTime.Now,Points=10,RuleTitle="参与活动",Remark="通过参与活动获得"},
                    new T_PointsRule(){ Code=PointsRule.SHARE.ToString(),Limit=10,CreateDate=DateTime.Now,Points=100,RuleTitle="分享活动",Remark="通过分享活动获得"},
                    new T_PointsRule(){ Code=PointsRule.PUBLISH.ToString(),Limit=3,CreateDate=DateTime.Now,Points=30,RuleTitle="发布活动",Remark="通过发布活动获得"},
                    new T_PointsRule(){ Code=PointsRule.REPORT.ToString(),Limit=99,CreateDate=DateTime.Now,Points=100,RuleTitle="举报",Remark="通过举报获得"},
                    new T_PointsRule(){ Code=PointsRule.FEEDBACK.ToString(),Limit=99,CreateDate=DateTime.Now,Points=100,RuleTitle="反馈",Remark="通过反馈获得"},
                    new T_PointsRule(){ Code=PointsRule.REGISTRATION.ToString(),Limit=1,CreateDate=DateTime.Now,Points=30,RuleTitle="签到",Remark="通过签到获得"},
                    new T_PointsRule(){ Code=PointsRule.BUY.ToString(),Limit=99,CreateDate=DateTime.Now,Points=10,RuleTitle="订购高级版",Remark="通过订购高级版获得"},
                    new T_PointsRule(){ Code=PointsRule.NOPRIZE.ToString(),Limit=99,CreateDate=DateTime.Now,Points=10,RuleTitle="活动未中奖",Remark="参与活动未中奖获取"},
                    new T_PointsRule(){ Code=PointsRule.INVITE.ToString(),Limit=3,CreateDate=DateTime.Now,Points=10,RuleTitle="邀请朋友参与",Remark="邀请朋友参与活动获得"}
                };
                List<T_VipModel> listVip = new List<T_VipModel>() {
                    new T_VipModel(){ Code=Vip.ONE.ToString(),Money=99,Value=1,Title="订购1天",StateCode=StateCode.Enable.ToString(),CreateDate=DateTime.Now },
                    new T_VipModel(){ Code=Vip.THREE.ToString(),Money=199,Value=3,Title="订购3天",StateCode=StateCode.Enable.ToString(),CreateDate=DateTime.Now },
                    new T_VipModel(){ Code=Vip.SEVEN.ToString(),Money=399,Value=7,Title="订购7天",StateCode=StateCode.Enable.ToString(),CreateDate=DateTime.Now }
                };
                context.T_PrizeModel.AddRange(listPrize);
                context.T_ExchangeModel.AddRange(listExchange);
                context.T_PointsRule.AddRange(listRule);
                context.T_VipModel.AddRange(listVip);

                context.SaveChanges();
            }

        }
    }
}
